package chamans.away
{
	import away3d.containers.ObjectContainer3D;
	import away3d.materials.BitmapMaterial;
	import away3d.entities.Mesh;
	import flash.geom.*;
	import com.bulletflash.dynamics.*;
	
	public class desk
	{
		private var BASE:Object;
		private var intern:Mesh;
		private var end:Function;
		private var Pos:Vector3D;
		private var mat:BitmapMaterial;
		private var m_liste:Vector.<Mesh>;
		private var r_liste:Vector.<btRigidBody>;
		
		public function desk(base:Object, END:Function=null, P:Vector3D=null)
		{
			BASE=base; end=END || new Function();
			BASE.LOAD.IMGLIST("assets/demobullet/desk/", ['desk_d', "desk_s", "desk_local" ], initMaterials , ".jpg")	
		}
		private function initMaterials( A:Array  ) : void
		{
			mat = new BitmapMaterial(A[0]);	
			mat.ambientColor = BASE.FogColor;
			mat.specularMap = A[1];
			mat.normalMap = A[2];
			mat.lights = BASE.LIGHT;
			
			mat.specular = 1.5;
			mat.ambient=.2;
			mat.gloss = 30;
			mat.name="Desk"
			if(BASE.reflect)BASE.F_reflect(mat, .5)
			if(BASE.fog) BASE.F_Fog(mat)	
			BASE.MatListe.push(mat);
			
			m_liste = new Vector.<Mesh>();
			r_liste = new Vector.<btRigidBody>();
			BASE.OBJ3D('assets/demobullet/desk/desk.obj', Define);
		}
		private function Define(obj:ObjectContainer3D) : void
		{
			intern = obj.getChildAt(0) as Mesh;
			intern.material=mat;
			BASE.Pause(end);end=null;
		}
		public function clone(p:Vector3D=null, rot:Vector3D=null) : void
		{
			var mesh:Mesh = intern.clone() as Mesh;
			if(p)Pos=p; else Pos=RanPos();
			if(BASE.phyOn) Physics(mesh, rot); else mesh.position=Pos;
			mesh.material=mat;
			BASE.view.scene.addChild(mesh);
			m_liste.push(mesh);
		}
		private function RanPos() : Vector3D {
			return new Vector3D(BASE.TOOL.Ran(1000, -1000), 37, BASE.TOOL.Ran(1000));
		}
		public function Delete() : void
		{
			if(BASE.phyOn){ while(r_liste.length){
				BASE.physics.world.removeRigidBody(r_liste[r_liste.length]);
				r_liste.pop();
			}}
			while(m_liste.length){
				BASE.view.scene.removeChild(m_liste[m_liste.length]);
				m_liste.pop();
			}
		}
		private function Physics(mesh:Mesh, r:Vector3D) : void
		{
			var phyData:Vector3D=new Vector3D(5, .3, .3);
			mesh=BASE.physics.Make("desk", m_liste, r_liste, Pos, new Vector3D(), phyData, mesh, false, r);
		}
		
	}}
